"""
encoding = 'utf-8'
@author : ffine
@time   : 2022-05-03  15:21
@IDEA   : PyCharm
@goal   : 正则表达式: 三大查找模式
"""
import re


"""
	match() 尝试从字符串的起始位置匹配一个模式，如果不是起始位置匹配成功的话，match()就返回None
"""
result = re.match(r'\d{3}-\d{3,8}', '010-12345')
print(result)  # <_sre.SRE_Match object; span=(0, 9), match='010-12345'>, 匹配成功
print(result.group())  # 获取匹配到的字符串

result = re.match(r'\d{3}-\d{3,8}', '010 12345')
print(result)  # None, 匹配失败

"""
	search() 扫描"整个字符串"并返回第一个成功的匹配
"""
print("{:=^50s}".format("search"))
result = re.search(r'\d{3}-\d{3,8}', 'a010-12345')
print(result)  # <_sre.SRE_Match object; span=(1, 10), match='010-12345'>, 匹配成功
print(result.group())  # 获取匹配到的字符串

result = re.search(r'\d{3}-\d{3,8}', '010 12345-213')
print(result)  # <re.Match object; span=(6, 13), match='345-213'>
print(result.group())  # 获取匹配到的字符串

result = re.search(r'\d{3}-\d{3,8}', '010 123453')
print(result)  # None, 匹配失败

"""
	findall() 扫描整个字符串并返回成功的匹配
"""
print("{:=^50s}".format("findall"))
result = re.findall(r'\d{3}\-\d{3,8}', '010 12345-213')
print(result)  # ['345-213']

result = re.findall(r'\d{3}\-\d{3,8}', '010 12345 213')
print(result)  # []

"""
# finditer() 扫描整个字符串并返回成功的匹配迭代器
"""
print("{:=^50s}".format("finditer"))
results = re.finditer(r'\d{3}\-\d{3,8}?', '010 12345-212335345-343-3434-3433')  # 返回迭代器
print(type(results))  # <class 'callable_iterator'>
print(results.__next__())  # <re.Match object; span=(6, 13), match='345-212'>
print(results.__next__())  # <re.Match object; span=(16, 23), match='345-343'>
print(results.__next__())  # <re.Match object; span=(25, 32), match='434-343'>
# print(results.__next__())  # StopIteration, 迭代器已经结束


"""
# split() 分割字符串
"""
print("{:=^50s}".format("split"))
print("1 2 3 4 5 6 7 8 9".split(' '))  # ['1', '2', '3', '4', '5', '6', '7', '8', '9']
result = re.split(r'[+\-\*/]', '1+9-2*3-4/5')
print(result)  # ['1', '9', '2', '3', '4', '5']

"""
# sub replace 字符串替换
"""
print("{:=^50s}".format("sub"))
result = re.sub(r'\d+', 'x', '1+9-2*3-4/5')  # 将数字替换成 x
print(result)  # -+-*-/-

print("{:=^50s}".format("sub"))
result = re.sub(r'\d+', 'x', '1+9-2*3-4/5', 2)  # 将数字替换成x, 第四个参数表示替换次数,只替换2次
print(result)  # x+x-2*3-4/5

"""
# 分组group
"""
print("{:=^50s}".format("group"))
result = re.match(r'h(.*)(\d{3})', 'hfad2323fadf2323')
print(result.group())  # hfad2323fadf2323

result = re.match(r'h(.*)(\d{3})', 'hfad2323fadf2323')
print(result.group())  # hfad2323fadf2323
print(result.group(0))  # hfad2323fadf2323
print(result.group(1))  # fad2323fadf2
print(result.group(2))  # fad2323fadf2

"""
# 反向引用,两句话互相转换
"""
print("{:=^50s}".format("反向引用"))
myLine = "i am <td>sniper<td>, i am <td>22<td> years old, i am from <td>china<td>"
result = re.sub(r'<td>(.*?)<td>', r'\1', myLine)
print(result)  # i am sniper, i am 22 years old, i am from china

anotherLine = "i am sniper, i am 22 years old, i am from china"
result = re.sub(r'(\w{3,})', r'<td>\1<\\td>', anotherLine)
print(result)  # i am <td>sniper<\td>, i am 22 <td>years<\td> <td>old<\td>, i am <td>from<\td> <td>china<\td>

"""
flag 参数
"""
print("{:=^50s}".format("flag 参数"))
result = re.findall(r's', 'Spider Man is peter parker')
print(result)  # ['s']

result = re.findall(r'\w?s\w+', 'Spider Man is peter parker', re.IGNORECASE)  # 忽略大小写
print(result)  # ['Spider']

parstr = """
<p>
	123
	456
	789
</p>
"""
print(re.findall(r"<p>(.*)</p>", parstr))  # []
print(re.findall(r"<p>(.*)</p>", parstr, re.S))  # ['\n\t123\n\t456\n\t789\n']; re.S 表示匹配任意换行符
